
#include "includes.h"
#include "structs.h"
#include "declares.h"
#include "sssp.h"

static char *doc="sssp-serial: a program that use serial Dijkstra algorithm to solve Single Source Shortest Paths Problem";
static char *args_doc="";

int main(int ac, char**av)
{
        grape_argp_parse(ac, av, doc, args_doc);
        grape_init(&sssp_plugin);
        grape_load_vertices();
        grape_load_edges();
        grape_build_global_adjlist();
        igraph_vector_int_t result;
        int vn=grape_get_vn(); 
        grape_load_queries();
        
        for(int i=0;i<grape_data.qn;++i) {

                zlog_info(grape_proc_logger, "processing query %d...", i+1);
                int query=VECTOR(grape_graph.all_queries)[i];

                /* main work */
                reset_timer(EACH_Q_TIMER);
                sssp_serial(&grape_graph.global_adjlist,
                                query,
                                &result);
                stop_timer(EACH_Q_TIMER);

                char *dat=va_format("%s.%s.query_%d.dat", arguments.output_file, grape_app.app_name, i+1);
                grape_file.dat=fopen(dat, "w");
                grape_errexit_if(!grape_file.dat, 1, GRAPE_EFILE, "output file", dat, "writing");
                zlog_info(grape_proc_logger, 
                                "query %d is processed in %lf s, result is written to %s", 
                                i+1,get_timer(EACH_Q_TIMER), dat);
                for(int i=0;i<vn;++i) {
                        fprintf(grape_file.dat, "%d\t%d\n", i+1, igraph_vector_int_e(&result,i));
                }
                fclose(grape_file.dat);
                free(dat);
        } 
        igraph_vector_int_destroy(&result);
        return 0;

}
